package v9.assist.analyst

import android.graphics.Bitmap
import sample.ui.Properties
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.TARGET
import kotlinx.coroutines.script.SMapBase.hitDeathRed
import kotlinx.coroutines.script.SMapBase.hitEquipO
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyBlue
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyYellow
import kotlinx.coroutines.script.SMapBase.hitTeammate
import kotlinx.coroutines.script.tes.ColorDiffer
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.rgb
import kotlinx.coroutines.script.tes.shake
import v9.assist.script.sLastFeature30Ts
import v9.assist.script.xBossBesideTs
import v9.assist.script.xLastAtkTs
import v9.assist.script.xLastPetBlacksmithTs
import v9.assist.script.xLastSceneEnterTs
import v9.assist.script.xLastUnknownTs
import kotlin.math.abs

private val hitRoad by lazy { // 21-28
    val out = mutableSetOf<ColorDiffer>()
    out.addAll(setOf(0xff3e3a32.toInt().rgb(12),)) // 起点区域 edge大点保证路线完整性
    out.addAll(setOf(0xff524d3e.toInt().rgb(12),)) // 小孩区域
    out.addAll(setOf(0xff38342c.toInt().rgb(12),)) // boss区域
    out.addAll(setOf(0xff41372d.toInt().rgb(16, 9, 9),)) // 仪式2
    out.addAll(hitEquipO)
    out.addAll(hitDeathRed)
    out.addAll(hitTeammate)
    out.addAll(hitSMapEnemyYellow)
    out.addAll(hitSMapEnemyBlue)
    out
}

var anaPathfindingModeJT = MODE_PATHFINDING_P1 // p1 p2
var anaLastSMapJTFeature1Ts = 0L // 地下起点
var anaLastSMapJTFeature2Ts = 0L // 地下仪式点
var anaLastSMapJTFeature3Ts = 0L // 直通 估计没处理
var anaLastSMapJTFeature4Ts = 0L //
var anaLastSMapJTFeature5Ts = 0L //
var anaLastSMapJTFeature6Ts = 0L // 副本起点
var anaLastSMapJTFeature7Ts = 0L // 伪P2
var counterFeature7 = 0 //
var counterFeature7Ts = 0L // 伪P2
var anaLastSMapJTFeature8Ts = 0L // 直通3
var anaLastSMapJTFeature9Ts = 0L // Boss终点

fun rasterizeMapJT(ss: Bitmap) : Array<IntArray>{
    return rasterizeMap(ss, hitRoad)
}

fun analysisMapJT(ss: Bitmap) {
    analysisMapElements(ss)

    val returnMapArray = rasterizeMapJT(ss)

    if (blockEliteEnemyBoss != null && mapEnemyList.isNotEmpty()) {
        // 优先Boss
        mapTargetList.add(mapEnemyList.random())
        printLog { "analyst target to boss" }
        lastSMapTargetToBossTs = System.currentTimeMillis()
        if (mapElementDistance(blockEliteEnemyBoss) < 30.pt) xBossBesideTs = System.currentTimeMillis()
    } else if (mapEnemyList.isNotEmpty()
        && System.currentTimeMillis() - anaLastPageSceneEnterTs > 10000 // 刚开局就需要
        && System.currentTimeMillis() - anaLastSceneSettleTs > 20000
        && !bsSceneSettle && !bsSceneSettleBlacksmith) {
        // 优先敌人
        mapTargetList.add(mapEnemyList.random())
        printLog { "analyst target to elite enemy" }
    } else if (bsSceneSettleBlacksmith && mapBlacksmithList.isNotEmpty()) {
        mapTargetList.add(mapBlacksmithList.random())
        printLog { "analyst target to blacksmith" }
    } else if (bsSceneSettle && mapBlueDoorList.isNotEmpty()) {
        mapTargetList.add(mapBlueDoorList.random())
        printLog { "analyst target to blueDoor" }
    } else if (mapRedDotList.size > 0 && anaPathfindingModeJT == MODE_PATHFINDING_P1) {
        mapTargetList.add(mapRedDotList.random())
        printLog { "analyst target to red dot" }
    } else if (mapNpcList.size > 0 && System.currentTimeMillis() - lastSMapTargetToBossTs > 8000) {
        mapTargetList.add(mapNpcList.random())
        printLog { "analyst target to npc" }
        lastSMapTargetToNpcTs = System.currentTimeMillis()
    } else if (mapSampleArrived.size > 0) {
        if (anaPathfindingModeJT == MODE_PATHFINDING_P1) {
            mapSampleArrived.minByOrNull { it.first + it.second }?.also {
                mapTargetList.add(it)
                printLog { "analyst target to sample 1 left top" } // left top
            }
        } else if (System.currentTimeMillis()- lastEliteEnemyBossTs < 8000
            && (!Properties.scenePetClear)) {
            // 非宠物分解 往上顶
            mapSampleArrived
                .filter { it.second < selfInSmallMap.y - smallMap.top }
                .minByOrNull { it.second }?.also {
                    mapTargetList.add(it)
                    printLog { "analyst target to sample top " }
                }
        } else if (System.currentTimeMillis()- lastEliteEnemyBossTs < 8000) {
            // 通关了 往右顶
            mapSampleArrived
                .filter { it.first > selfInSmallMap.x - smallMap.left }
                .minByOrNull { it.second }?.also {
                    mapTargetList.add(it)
                    printLog { "analyst target to sample right top" }
                }
        } else {
            mapSampleArrived.maxByOrNull { it.first * 2 - it.second }?.also {
                mapTargetList.add(it)
                printLog { "analyst target to sample 2" } // right right top
            }
        }
    }

    if (mapTargetList.isNotEmpty()) {
        val target = mapTargetList.random()
        returnMapArray[target.first][target.second] = TARGET
    }

    angleByRoad = analysisRasterizeMap(returnMapArray)
    angleByRoad = angleSpecialAdjust(angleByRoad ?: (if (anaPathfindingModeJT == MODE_PATHFINDING_P2) 50.shake(10) else 330.shake(10)))

    // map feature
    analysisMapJTIsStartPoint()
    if ((blockBlueDoor == null || blockBlacksmith == null)
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000) {
        printLog { "analystJT $mapSimpleStatus" }
        if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.58 .. 0.86
            && lastMapBottomC in 2 .. 56
            && lastMapLeftP == null
            && System.currentTimeMillis() - lastBlacksmithTs > 20000 // 可能跑到地图外面了
            && System.currentTimeMillis() - xLastUnknownTs > 10000 // 副本入口
            && System.currentTimeMillis() - xLastSceneEnterTs > 10000
            && System.currentTimeMillis() - lastBlacksmithTs > 10000 // 副本外的误判.用这个屏蔽
        ) {
            // [上,43s,c:0][下,0s,0.607677,c:17][左,73s,c:0][右,6s,c:0]][Road,c:19]
            // [上,1737473915s,c:0][下,0s,0.8237534,c:46][左,1737473915s,c:0][右,2s,c:0]][Road,c:18]
            // [上,46s,c:0][下,0s,0.82665634,c:44][左,110s,c:0][右,0s,0.95584047,c:6]][Road,c:18]
            // [上,52s,c:0][下,0s,0.6581921,c:4][左,82s,c:0][右,0s,0.6511471,c:19]][Road,c:22]
            // [上,61s,c:0][下,0s,0.7359588,c:17][左,91s,c:0][右,4s,c:0]][Road,c:18]
            // [上,70s,c:0][下,0s,0.6479862,c:31][左,101s,c:0][右,13s,c:0]][Road,c:14]
            printLog { "analystJT 节点.地下起点" }
            anaLastSMapJTFeature1Ts = System.currentTimeMillis()
            anaPathfindingModeJT = MODE_PATHFINDING_P2
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.55 .. 0.72
            && (lastMapBottomP == null || (lastMapBottomP ?: -1f) in 0.26 .. 0.48)
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastSMapJTFeature4Ts > 3000
            && System.currentTimeMillis() - lastBlueDoorTs > 6000
        ) {
            // [上,0s,0.60624224,c:41][下,2s,c:0][左,20s,c:0][右,11s,c:0]][Road,c:14]
            // [上,0s,0.63847345,c:51][下,0s,0.34129432,c:22][左,19s,c:0][右,39s,c:0]][Road,c:34]
            // [上,0s,0.6442649,c:43][下,0s,0.40659133,c:15][左,44s,c:0][右,65s,c:0]][Road,c:34]
            // [上,0s,0.60624224,c:41][下,17s,c:0][左,65s,c:0][右,56s,c:0]][Road,c:14]
            printLog { "analystJT 节点.地下仪式2" }
            anaLastSMapJTFeature2Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.35 .. 0.44
            && lastMapBottomP == null
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - xLastAtkTs > 4000
            && System.currentTimeMillis() - lastBlueDoorTs < 2000
            && System.currentTimeMillis() - xLastUnknownTs > 6000
            && System.currentTimeMillis() - xLastSceneEnterTs > 15000
        ) {
            printLog { "analystJT 节点.直通2" }
            // [上,0s,0.40729854,c:49][下,1737714748s,c:0][左,1737714748s,c:0][右,1737714748s,c:0]][Road,c:11]
            // [上,0s,0.40729854,c:49][下,36s,c:0][左,329s,c:0][右,342s,c:0]][Road,c:11]
            anaLastSMapJTFeature5Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.28 .. 0.34
            && lastMapBottomP == null
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - xLastAtkTs > 4000
            && System.currentTimeMillis() - lastBlueDoorTs < 2000
            && System.currentTimeMillis() - xLastUnknownTs > 6000
            && System.currentTimeMillis() - xLastSceneEnterTs > 15000
        ) {
            printLog { "analystJT 节点.直通3" }
            // [上,0s,0.40729854,c:49][下,1737714748s,c:0][左,1737714748s,c:0][右,1737714748s,c:0]][Road,c:11]
            // [上,0s,0.40729854,c:49][下,36s,c:0][左,329s,c:0][右,342s,c:0]][Road,c:11]
            anaLastSMapJTFeature8Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.68 .. 0.84
            && lastMapRoadC < 18 // 实际5-9
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 12000) {
            // [上,1737556383s,c:0][下,0s,0.7542373,c:24][左,1737556383s,c:0][右,1737556383s,c:0]][Road,c:7]
            printLog { "analystJT 节点.副本外入口 " } // maybe//  [可以用sampleCount来区分] // boss前也有这个
            anaLastSMapJTFeature4Ts = System.currentTimeMillis()
            anaClearSceneSettle()
            anaPathfindingModeJT = MODE_PATHFINDING_P1 // p1 p2

        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.48 .. 0.72
            && (lastMapBottomP ?: -1f) in 0.36 .. 0.54
            && (lastMapLeftP ?: -1f) in 0.19 .. 0.33
            && lastMapRightP == null
            && System.currentTimeMillis() - xLastAtkTs > 2000) {
            // [上,0s,0.5290856,c:54][下,0s,0.50598204,c:68][左,0s,0.23620825,c:44][右,26s,c:0]][Road,c:49]
            // [上,0s,0.665456,c:42][下,0s,0.4235943,c:42][左,0s,0.2825482,c:43][右,19s,c:0]][Road,c:46]
            // [上,0s,0.5571618,c:51][下,0s,0.40960452,c:43][左,0s,0.24131054,c:45][右,41s,c:0]][Road,c:46]
            // [上,0s,0.66011536,c:47][下,0s,0.4153798,c:45][左,0s,0.2625418,c:46][右,52s,c:0]][Road,c:46]
            // [上,0s,0.7084385,c:47][下,0s,0.43220338,c:37][左,0s,0.27583528,c:44][右,0s,c:0]][Road,c:47]
            if (System.currentTimeMillis() - counterFeature7Ts > 6000) counterFeature7 = 0
            counterFeature7++
            counterFeature7Ts = System.currentTimeMillis()
            printLog { "analystJT 节点.副本伪P2回头 $counterFeature7" }
            if (counterFeature7 > 12) {
                printLog { "analystJT 节点.副本伪P2回头 切换到P1" }
                anaLastSMapJTFeature7Ts = System.currentTimeMillis()
                counterFeature7 = 0
            }
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.07 .. 0.16
            && (lastMapLeftP ?: -1f) in 0.86 .. 0.96
            && lastMapRightP == null
            && System.currentTimeMillis() - lastBlueDoorTs > 4000) {
            // [上,39s,c:0][下,0s,0.10551678,c:34][左,0s,0.9055944,c:11][右,50s,c:0]][Road,c:32]
            // [上,90s,c:0][下,0s,0.10169491,c:32][左,0s,0.9055944,c:11][右,100s,c:0]][Road,c:32]
            // [上,107s,c:0][下,0s,0.13000919,c:43][左,0s,0.9358974,c:2][右,118s,c:0]][Road,c:30]
            printLog { "analystJT 节点.Boss终点" }
            anaLastSMapJTFeature9Ts = System.currentTimeMillis()
        }
    }


    if (System.currentTimeMillis() - anaLastPageSceneEnterTs > 8000
        && System.currentTimeMillis() - xLastUnknownTs > 8000
        && System.currentTimeMillis() - anaLastHasEnemyNameTs > 4000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 4000
        && blockBlueDoor != null
        && blockBlacksmith != null
        && !bsSceneSettle && !bsSceneSettleBlacksmith
        && simpleAssertSceneCompleted(blockBlueDoor, blockBlacksmith)
        // 这逻辑是错的.有几率往回跑.不带这个 && System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
        && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000) { // 特有判定
        printLog { "analyst scene completed count $anaCounterSceneSettle" }
        anaCounterSceneSettle++
        if (anaCounterSceneSettle > 0) {
            printLog { "analyst scene completed" }
            anaMarkSceneSettle()
        }
    }
    if (System.currentTimeMillis() - lastBlueDoorTs > 3000
        && System.currentTimeMillis() - lastBlacksmithTs > 3000) {
        anaCounterSceneSettle = 0
    }
}

fun analysisMapJTIsStartPoint(): Boolean {
    if ((blockBlueDoor == null || blockBlacksmith == null)
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000) {
        if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.14 .. 0.26
            && lastMapBottomP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - lastBlueDoorTs < 3000) {
            // [上,0s,0.20803069,c:56][下,45s,c:0][左,0s,0.07753357,c:7][右,324s,c:0]][Road,c:27]
            // [上,0s,0.21305382,c:38][下,61s,c:0][左,237s,c:0][右,219s,c:0]][Road,c:32]
            // [上,0s,0.20987937,c:37][下,45s,c:0][左,33s,c:0][右,1746165808s,c:0]][Road,c:32]
            printLog { "analystJT 节点.教堂副本起点A " } // maybe//  [可以用sampleCount来区分] // boss前也有这个
            anaLastSMapJTFeature6Ts = System.currentTimeMillis()
            anaPathfindingModeJT = MODE_PATHFINDING_P1
            return true
        }
    }
    return false
}

private fun simpleAssertSceneCompleted(blueDoor: Block?, blacksmith: Block?): Boolean {
    if (blueDoor == null) return false
    if (blacksmith == null) return false
    val edge = 80.pt
    return (abs(blueDoor.x - blacksmith.x) < edge && abs(blueDoor.y - blacksmith.y) < edge)
}


private fun angleSpecialAdjust(angle: Int): Int {
    if (System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 3000
        && System.currentTimeMillis() - lastEliteEnemyTs > 3000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 3000
        && System.currentTimeMillis() - lastMapRedDotTs > 3000
        && anaPathfindingModeJT == MODE_PATHFINDING_P2
        && System.currentTimeMillis() - xLastAtkTs < 600
        && System.currentTimeMillis() - sLastFeature30Ts > 1500) {
        printLog { "analystJT 回撤清理漏怪" }
        sLastFeature30Ts = System.currentTimeMillis()
        return (angle + 180) % 360
    }

    if (angle in 0 .. 280) {
        if (System.currentTimeMillis() - xLastAtkTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyTs > 5000
            && System.currentTimeMillis() - anaLastSMapJTFeature1Ts > 20000 // 地图特征 条件
            && System.currentTimeMillis() - anaLastSMapJTFeature2Ts > 20000
            && System.currentTimeMillis() - xLastSceneEnterTs > 30000
            && System.currentTimeMillis() - xLastPetBlacksmithTs > 20000
            && System.currentTimeMillis() - xLastUnknownTs < 20000) {
            printLog { "analystJT angleSpecialAdjust $angle to 320" }
            return 320
        }
    }
    return angle
}